Return to start page
Systems/Character/Struct Focus.j
1 /// Do not use this library, it is unfinished!
2 library AStructSystemsCharacterFocus requires optional ALibraryCoreDebugMisc, AStructCoreGeneralHashTable, ALibraryCoreMathsRect, ALibraryCoreMathsHandle, ALibraryCoreInterfaceMisc, ALibraryCoreInterfaceTextTag, ALibraryCoreEnvironmentUnit, AStructSystemsCharacterAbstractCharacterSystem
3
4 struct AFocus extends AAbstractCharacterSystem
5 //static start members
6 private static real refreshRate
7 private static real range
8 private static real angle
9 private static integer workerUnitType
10 private static boolean showText
11 private static boolean indicateTarget
12 private static string textLevel
13 //members
14 private trigger focusTrigger
15 private texttag textTag
16 private trigger workerTrigger
17 private unit worker
18 private unit target
19 private boolean fixedTarget
20
21 //! runtextmacro optional A_STRUCT_DEBUG("\"AFocus\"")
22
23 public method enable takes nothing returns nothing
24 call super.enable()
25 call EnableTrigger(this.focusTrigger)
26 call ShowTextTagForPlayer(this.user(), this.textTag, true)
27 call EnableTrigger(this.workerTrigger)
28 endmethod
29
30 public method disable takes nothing returns nothing
31 call super.disable()
32 call DisableTrigger(this.focusTrigger)
33 call ShowTextTagForPlayer(this.user(), this.textTag, false)
34 call DisableTrigger(this.workerTrigger)
35 endmethod
36
37 private method isTargetInFocus takes nothing returns boolean
38 if (GetDistanceBetweenUnits(this.unit(), this.target, 0.0, 0.0) > AFocus.range) then
39 //Optional kann man auch mit Z-Wert (Extrafunktion) überprfen lassen, würde aber mehr Speicher ziehen
40 return false
41 //Erst überprfen
42 elseif (GetAngleBetweenUnits(this.unit(), this.target) > AFocus.angle) then
43 return false
44 endif
45 return true
46 endmethod
47
48 //Don't destroy the group!
49 private method getNearestTarget takes group usedGroup returns unit
50 local unit first = null
51 local unit nearest = null
52 loop
53 set first = FirstOfGroup(usedGroup)
54 exitwhen (first == null)
55 if (this.unit() != first) then //Darf sich nicht selbst anvisieren
56 if ((nearest == null) or (GetDistanceBetweenUnits(this.unit(), first, 0.0, 0.0) < GetDistanceBetweenUnits(this.unit(), nearest, 0.0, 0.0))) then //ALibraryMathsHandle
57 set nearest = first
58 endif
59 endif
60 call GroupRemoveUnit(usedGroup, first)
61 set first = null
62 endloop
63 return nearest
64 endmethod
65
66 private method getTargetName takes nothing returns string
67 local integer state = GetUnitAllianceStateToUnit(this.unit(), this.target)
68 local string colour
69 if (state == bj_ALLIANCE_UNALLIED) then
70 set colour = "|c00ff0000"
71 elseif (state == bj_ALLIANCE_ALLIED) then
72 set colour = "|c0000ff00"
73 else //Neutral
74 set colour = "|c00ffcc00"
75 endif
76 return (colour + GetUnitName(this.target) + "|r " + AFocus.textLevel + ":" + I2S(GetUnitLevel(this.target)))
77 endmethod
78
79 private method showTargetText takes nothing returns nothing
80 if (AFocus.showText) then
81 call SetTextTagTextBJ(this.textTag, this.getTargetName(), 12.0)
82 call SetTextTagPos(this.textTag, GetUnitX(this.target), GetUnitY(this.target), (GetUnitFlyHeight(this.target) + 70.0))
83 call ShowTextTagForPlayer(this.user(), this.textTag, true)
84 endif
85 endmethod
86
87 private method indicateTheTarget takes real red, real green, real blue, real alpha returns nothing
88 if (AFocus.indicateTarget) then
89 call SetUnitVertexColourForPlayer(this.user(), this.target, red, green, blue, alpha)
90 endif
91 endmethod
92
93 private method getNewTarget takes nothing returns nothing
94 local real x1 = GetUnitX(this.unit())
95 local real y1 = GetUnitY(this.unit())
96 local real x3 = GetUnitPolarProjectionX(this.unit(), (GetUnitFacing(this.unit()) + AFocus.angle), AFocus.range) //ALibraryMathsHandle
97 local real y3 = GetUnitPolarProjectionY(this.unit(), (GetUnitFacing(this.unit()) + AFocus.angle), AFocus.range) //ALibraryMathsHandle
98 local real x4 = GetUnitPolarProjectionX(this.unit(), (GetUnitFacing(this.unit()) - AFocus.angle), AFocus.range) //ALibraryMathsHandle
99 local real y4 = GetUnitPolarProjectionY(this.unit(), (GetUnitFacing(this.unit()) - AFocus.angle), AFocus.range) //ALibraryMathsHandle
100 local group targetGroup = GetGroupInRectByCoordinates(x1, y1, x1, y1, x3, y3, x4, y4)
101 debug if (IsUnitGroupEmptyBJ(targetGroup)) then
102 debug call Print("Group is empty")
103 debug else
104 debug call Print("Group is not empty")
105 debug endif
106 set this.target = this.getNearestTarget(targetGroup) //Gruppe wird verändert
107 call DestroyGroup(targetGroup)
108 set targetGroup = null
109 if (this.target == null) then
110 call ShowTextTagForPlayer(this.user(), this.textTag, false)
111 return
112 endif
113 call this.showTargetText()
114 call this.indicateTheTarget(100.0, 50.0, 50.0, 0.0)
115 endmethod
116
117 private static method triggerActionFocus takes nothing returns nothing
118 local trigger triggeringTrigger = GetTriggeringTrigger()
119 local AFocus this = AHashTable.global().handleInteger(triggeringTrigger, "this")
120 //Hat bereits ein Ziel
121 if (this.target != null) then
122 //Altes Objekt ist außer Reichweite - Bentige neues Ziel
123 if (not this.isTargetInFocus()) then
124 call this.indicateTheTarget(100.0, 100.0, 100.0, 0.0)
125 call this.getNewTarget()
126 endif
127 //Hat kein Ziel
128 else
129 call this.getNewTarget()
130 endif
131 set triggeringTrigger = null
132 endmethod
133
134 private method createFocusTrigger takes nothing returns nothing
135 local event triggerEvent
136 local triggeraction triggerAction
137 set this.focusTrigger = CreateTrigger()
138 set triggerEvent = TriggerRegisterTimerEvent(this.focusTrigger, AFocus.refreshRate, true)
139 set triggerAction = TriggerAddAction(this.focusTrigger, function AFocus.triggerActionFocus)
140 call AHashTable.global().setHandleInteger(this.focusTrigger, "this", this)
141 set triggerEvent = null
142 set triggerAction = null
143 endmethod
144
145 private method createTextTag takes nothing returns nothing
146 set this.textTag = CreateTextTag()
147 call SetTextTagVisibility(this.textTag, false)
148 endmethod
149
150 private method createWorkerTrigger takes nothing returns nothing
151 local event triggerEvent
152 local triggeraction triggerAction
153 //set this.worker = CreateUnit(this.getCharacter().user(), AFocus.workerUnitType,
154
155 set this.workerTrigger = CreateTrigger()
156 //Noch ausarbeiten
157 endmethod
158
159 public static method create takes ACharacter character returns AFocus
160 local AFocus this = AFocus.allocate(character)
161 //members
162 set this.fixedTarget = false
163
164 call this.createFocusTrigger()
165 call this.createTextTag()
166 call this.createWorkerTrigger()
167 return this
168 endmethod
169
170 private method destroyFocusTrigger takes nothing returns nothing
171 call AHashTable.global().destroyTrigger(this.focusTrigger)
172 set this.focusTrigger = null
173 endmethod
174
175 private method destroyTextTag takes nothing returns nothing
176 call DestroyTextTag(this.textTag)
177 set this.textTag = null
178 endmethod
179
180 private method destroyWorkerTrigger takes nothing returns nothing
181 call RemoveUnit(this.worker)
182 set this.worker = null
183 call AHashTable.global().destroyTrigger(this.workerTrigger)
184 set this.workerTrigger = null
185 endmethod
186
187 public method onDestroy takes nothing returns nothing
188
189 call this.destroyFocusTrigger()
190 call this.destroyTextTag()
191 call this.destroyWorkerTrigger()
192 endmethod
193
194 /**
195 * @param refreshRate 1.0 //If this value is 0.0 there is no text
196 * @param range 500.0
197 * @param angle 30.0 //Grad 0-360
198 * @param workerUnitType 'HPEA'
199 * @param showText true
200 * @param indicateTarget true
201 * @param textLevel Stufe
202 */
203 public static method init takes real refreshRate, real range, real angle, integer workerUnitType, boolean showText, boolean indicateTarget, string textLevel returns nothing
204 //static start members
205 set AFocus.refreshRate = refreshRate
206 set AFocus.range = range
207 set AFocus.angle = angle
208 set AFocus.workerUnitType = workerUnitType
209 set AFocus.showText = showText
210 set AFocus.indicateTarget = indicateTarget
211 set AFocus.textLevel = textLevel
212 endmethod
213 endstruct
214
215 endlibrary